package edu.zjut.sjq.controller;

import edu.zjut.sjq.bean.Article;
import edu.zjut.sjq.bean.Result;
import edu.zjut.sjq.bean.User;
import edu.zjut.sjq.bean.Year;
import edu.zjut.sjq.service.ArticleService;
import edu.zjut.sjq.service.UserService;
import edu.zjut.sjq.service.YearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by sue on 2017/3/12.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private YearService yearService;
    @Autowired
    private HttpSession session;

    // 登录
    @RequestMapping(value = "/login",produces="application/json")
    @ResponseBody
    public Result login(@RequestBody User user){
        Result result= userService.login(user);
        return result;
    }

    //退出
    @RequestMapping(value = "/logout",produces="application/json")
    @ResponseBody
    public Result logout(){
        Result result=new Result();
        result.setRes(1);
        result.setMsg("退出成功");
        session.removeAttribute("user");
        return result;
    }


    // 注册
    @RequestMapping(value = "/register",produces="application/json")
    @ResponseBody
    public Result register(@RequestBody User user){
        Result result= userService.register(user);
        return result;
    }


    // 修改用户信息
    @RequestMapping(value = "/update",produces = "application/json")
    @ResponseBody
    public Result updateInfo(@RequestBody User user){
        return userService.updateInfo(user);
    }

    // 加载主页
    @RequestMapping(value = "/loadLatest")
    public ModelAndView loadLatest() throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("latest.jsp");
        List<Article> articleList = articleService.getLatestArticle(6,null);
        modelAndView.addObject("articleList",articleList);
        return modelAndView;
    }

    // 加载作品
    @RequestMapping(value = "/loadLatestArticle")
    public ModelAndView loadLatestArticle() throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        List<Article> articleList = articleService.getLatestArticle(6,null);
        modelAndView.setViewName("article.jsp");
        modelAndView.addObject("articleList",articleList);
        return modelAndView;
    }


    // 根据type加载作品页
    @RequestMapping(value = "/loadArticleByType")
    public ModelAndView loadArticleByType(@RequestParam Integer type){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("article.jsp");
        List<Article> articleList;
        if (type==0)
            articleList = articleService.getArticleListOrderByTime(null,null,null,null);
        else
            articleList = articleService.getArticleListOrderByTime(null,type,null,null);

        String title="";
        switch (type){
            case 0:
                title="全部";
                break;
            case 1:
                title="DV作品";
                break;
            case 2:
                title="动画作品";
                break;
            case 3:
                title="平面作品";
                break;
            case 4:
                title="网页作品";
                break;
            case 5:
                title="移动应用";
                break;
            case 6:
                title="微课作品";
                break;
            case 7:
                title="课件作品";
                break;
            default:
                title="全部";
                break;
        }
        modelAndView.addObject("articleList",articleList);
        modelAndView.addObject("title",title);
        return modelAndView;
    }

    // 根据届数加载作品页面
    @RequestMapping(value = "/loadArticleByYear")
    public ModelAndView loadArticleByYear(@RequestParam Integer year){
        ModelAndView modelAndView=new ModelAndView();
        List<Article> articleList;
        if (year==0)
            articleList = articleService.getArticleListOrderByTime(null,null,null,null);
        else
            articleList = articleService.getArticleListOrderByTime(year,null,null,null);

        String title= yearService.selectByYear(year).getName();
        modelAndView.setViewName("article.jsp");
        modelAndView.addObject("articleList",articleList);
        modelAndView.addObject("title",title);
        return modelAndView;
    }



}
